package com.workmanager.service;


import com.workmanager.bean.ClassJobTaskBean;
import com.workmanager.bean.JobTaskBean;
import com.workmanager.bean.WorkBean;
import com.workmanager.mapper.TeacherWorkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LENOVO on 2016/8/29.
 * 教师接收作业的service
 */
@Service("teacherWorkService")
public class TeacherWorkService {
    @Autowired
     private TeacherWorkMapper teacherWorkMapper;
    /**
     * 实现获取所有作业任务的接收，数据在receiveWork.jsp页面显示
     */
      public List<ClassJobTaskBean> getAllJobTask(String teacherId){
          List<ClassJobTaskBean> list = teacherWorkMapper.getAllJobTask(teacherId);
          return list;
      }
    /**
     * 实现获取指定搜索作业任务的接收，数据在receiveWork.jsp页面显示
     */
    public List<ClassJobTaskBean> getJobTask(String teacherId,String text){
        System.out.println("text-------"+text);
        if(text == ""){
            List<ClassJobTaskBean> list = teacherWorkMapper.getAllJobTask(teacherId);
            return list;
        }
        Map<String,String> para = new HashMap();
        para.put("teacherId",teacherId);
        para.put("text",text);
        List<ClassJobTaskBean> list = teacherWorkMapper.getJobTask(para);
        return list;
    }

    /**
     * 实现获取每一个作业任务的上交人数
     */
    public Map<String,String> getJobTaskNum(List<ClassJobTaskBean> list){
        Map<String,String> para = new HashMap<String, String>();
        for(ClassJobTaskBean job : list){
            para.put(job.getaJobTask().getJobTask_id(),teacherWorkMapper.getJobTaskNum(job.getaJobTask().getJobTask_id()));
        }
        return para;
    }
    /**
     * 实现获取到某一作业任务编号的所有作业
     */
    public List<WorkBean> getAllwork(String jobTaskId){
        List<WorkBean> list = teacherWorkMapper.getAllwork(jobTaskId);
        return list;
    }
    /**
     * 获取指定班级的总人数
     */
    public int getTotalPerson(String jobtaskId) {
        int total = Integer.parseInt(teacherWorkMapper.getTotalPerson(jobtaskId));
        return total;
    }
    /**
     * 获取某一指定作业任务标号的作业上交人数，workDetail.jsp中的上交人数
     */
    public int getTotalReport(String jobtaskId){
        int total = teacherWorkMapper.getTotalReport(jobtaskId);
        return total;
    }
    /**
     * 获取未交人数
     */
    public int getTotalUnreport(String jobtaskId){
        int total = getTotalPerson(jobtaskId) - getTotalReport(jobtaskId);
        return total;
    }
    /**
     * 获取某一指定作业编号的作业情况,workdetail.jsp的表头信息
     */
    public ClassJobTaskBean getAbyId(String workId){
        ClassJobTaskBean work = teacherWorkMapper.getAbyId(workId);
        return work;
    }
    /**
     * 查找某一学生的某一作业细节，在evaluateWork.jsp页面显示.
     * s.student_id,s.student_name,s.student_number,w.work_id,w.work_content,w.work_goodjob
     */
    public WorkBean getStudentWork(String workId){
        WorkBean workBean = teacherWorkMapper.getStudentWork(workId);
        return workBean;
    }
    public String getFileName(String workId){
        WorkBean workBean = teacherWorkMapper.getStudentWork(workId);
        return workBean.getWork_file();
    }
    /**
     * 对评价内容进行插入
     */
    public boolean doEvaluate(String workId,String text){
        Map<String,String> para = new HashMap<String, String>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        para.put("workId",workId);
        para.put("text",text);
        para.put("time",df.format(new Date()));
        boolean result = teacherWorkMapper.doEvaluate(para);
        return result;
    }
    /**
     * 分享优秀作业的操作
     */
    public boolean setGoodJob(String studentId,String workId,String isGood){
        Map<String,String> para = new HashMap<String, String>();
        para.put("studentId",studentId);
        para.put("workId",workId);
        para.put("good",isGood);
        boolean result = teacherWorkMapper.setGoodJob(para);
        return result;
    }

    /**
     * 老师下载文件
     */
    public boolean download(OutputStream out, String path, String filename, HttpServletRequest request, HttpServletResponse response){
        String[] a = filename.split("\\\\");
        System.out.println(a[a.length-1]);
        response.setContentType(request.getServletContext().getMimeType(filename));
        response.setHeader("Content-Disposition","attachment;filename="+a[a.length-1]);
        boolean result = false;
        InputStream in = null;
        try {
            in = new FileInputStream(path);
            byte [] buf=new byte[1024];
            int len = 0;
            while((len=in.read(buf))!=-1) {
                out.write(buf,0,len);
            }
            result = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public TeacherWorkMapper getTeacherWorkMapper() {
        return teacherWorkMapper;
    }

    public void setTeacherWorkMapper(TeacherWorkMapper teacherWorkMapper) {
        this.teacherWorkMapper = teacherWorkMapper;
    }
}
